﻿using System;
using System.Linq;
using System.IO;
using System.IO.IsolatedStorage;
using System.Collections.Generic;
using Microsoft.LightSwitch;
using Microsoft.LightSwitch.Framework.Client;
using Microsoft.LightSwitch.Presentation;
using Microsoft.LightSwitch.Presentation.Extensions;

namespace LightSwitchApplication
{
    public partial class CreateNewSalesOrder
    {
        partial void CreateNewSalesOrder_InitializeDataWorkspace(List<IDataService> saveChangesTo)
        {
            this.SalesOrderProperty = new SalesOrder();
            if (this.ShopId.HasValue)
            {
                this.SalesOrderProperty.OrderShop = this.DataWorkspace.ApplicationData.Shops.Where(e => e.Id == this.ShopId).FirstOrDefault();
                this.FindControl("OrderShop").IsReadOnly = true;
            }
            else if (this.Application.User.HasPermission(Permissions.CanEditSalesOrder))//admin
            {
            }
            //else if (this.Application.User.HasPermission(Permissions.CanManageMySalesOrder))
            //{
            //    //for some reason LINQ does not work with external datasource link
            //    //var currentSalesPerson = this.DataWorkspace.ApplicationData.SalesPersons.Where(s => s.aspnet_Profile.PropertyValuesString == userName).FirstOrDefault();
            //    //if (currentSalesPerson != null)
            //    //{
            //    //    this.SalesPersonId = currentSalesPerson.Id;
            //    //    this.SalesOrderProperty.VisitSalesPerson = currentSalesPerson;
            //    //    this.FindControl("VisitSalesPerson").IsReadOnly = true;
            //    //}

            //    string userName = this.Application.User.FullName;
            //    //sales person shops
            //    foreach (SalesPerson item in this.DataWorkspace.ApplicationData.SalesPersons)
            //    {
            //        if (item.aspnet_Profile != null && item.aspnet_Profile.PropertyValuesString == userName)
            //        {
            //            this.SalesPersonId = item.Id;
            //            //this.SalesOrderProperty.OrderSalesPerson = this.DataWorkspace.ApplicationData.SalesPersons.Where(e => e.Id == this.SalesPersonId).FirstOrDefault();
            //            //this.FindControl("VisitSalesPerson").IsReadOnly = true;
            //            break;
            //        }
            //    }
            //    //customer shops
            //    foreach (Customer item in this.DataWorkspace.ApplicationData.Customers.Where(c => c.aspnet_Profile_UserId != null))
            //    {
            //        if (item.aspnet_Profile != null && item.aspnet_Profile.PropertyValuesString == userName)
            //        {
            //            this.CustomerId = item.Id;
            //            //this.FindControl("VisitSalesPerson").IsReadOnly = true;
            //            break;
            //        }
            //    }
            //}
        }

        partial void CreateNewSalesOrder_Saved()
        {
            // Write your code here.
            this.Close(false);
            Application.Current.ShowSalesOrderDetail(this.SalesOrderProperty.Id);
        }

        partial void CreateNewSalesOrder_Activated()
        {
            try
            {
                this.FindControl("Save").IsEnabled = (this.Application.User.HasPermission(Permissions.CanEditSalesOrder)
                        || this.Application.User.HasPermission(Permissions.CanManageMySalesOrder)
                    );
            }
            catch (Exception)
            {
            }

        }

        partial void Cancel_Execute()
        {
            this.Close(false);
        }
    }
}